
export default class EventMgr {
    private static _instance: EventMgr;
    public static get Inst(): EventMgr {
        if (this._instance == undefined) {
            this._instance = new EventMgr();
        }
        return this._instance;
    }

    private funcList: { [x: string]: Function[] } = {};

    on(key: string | number, func: Function) {
        if (this.funcList[key] == undefined) {
            this.funcList[key] = [];
        }
        this.funcList[key].push(func);
    }

    off(key: string | number, func: Function) {
        let list = this.funcList[key];
        if (list) {
            let index = list.indexOf(func);
            if (index != -1) {
                list.splice(index, 1);
            }
        }
    }

    emit(key: string | number, ...param: any) {
        let list = this.funcList[key];
        if (list) {
            for (const func of list) {
                func(...param);
            }
        }
    }

    clear(key: string | number) {
        delete this.funcList[key];
    }

    flush() {
        this.funcList = {};
    }
}